package com.gitee.baqkft.code.ui.utils;

import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.common.serialization.StringDeserializer;
import org.apache.kafka.common.serialization.StringSerializer;

import java.util.HashMap;
import java.util.Map;

/**
 * 配置工厂
 * @Author: chenmingcai
 * @Date 2022/12/20 18:02
 **/

public abstract class ConfigFactory
{
    /**
     * Kafka消费者配置
     * @param servers
     * @param groupId
     * @return
     */
    public static Map<String, Object> getKafkaConsumerConfig(String servers, String groupId)
    {
        Map<String, Object> options = new HashMap<>();
        options.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, servers);
        options.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());
        options.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());
        options.put("batch.size", 16384);
        options.put("buffer.memory", 33554432);
        options.put("group.id", groupId);
        return options;
    }

    /**
     * Kafka生产者配置
     * @param servers
     * @return
     */
    public static Map<String, Object> getKafkaProducerConfig(String servers)
    {
        Map<String, Object> options = new HashMap<>();
        options.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, servers);
        options.put(ProducerConfig.ACKS_CONFIG, "all");
        options.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
        options.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
        options.put("batch.size", 16384);
        options.put("buffer.memory", 33554432);
        return options;
    }
}
